<template>
  <div class="">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/" style="font-size: 12px">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="10">
        <el-col :span="6">
          <el-input placeholder="请输入内容" class="input-with-select">
            <el-button icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="addRoles">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table border :data="rolesList" style="margin-top: 10px">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <div class="box">
              <div v-if="scope.row.children.length > 0">
                <el-row v-for="item1 in scope.row.children" :key="item1.id">
                  <el-col :span="3" class="col1">
                    <el-tag
                      closable
                      @close="delTag(scope.row, scope.row.id, item1.id)"
                      >{{ item1.authName }}</el-tag
                    >
                  </el-col>
                  <el-col :span="22" class="col2">
                    <el-row v-for="item2 in item1.children" :key="item2.id">
                      <el-col :span="2">
                        <el-tag
                          closable
                          type="warning"
                          @close="delTag(scope.row, scope.row.id, item2.id)"
                          >{{ item2.authName }}</el-tag
                        >
                      </el-col>
                      <el-col :span="20">
                        <el-tag
                          @close="delTag(scope.row, scope.row.id, item3.id)"
                          closable
                          type="danger"
                          v-for="item3 in item2.children"
                          :key="item3.id"
                        >
                          {{ item3.authName }}
                        </el-tag>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </div>
              <div v-else>此角色暂无数据</div>
            </div>
          </template>
          <template>
            <el-form label-position="left" inline class="demo-table-expand">
            </el-form>
          </template>
        </el-table-column>

        <el-table-column label="#" type="index" width="40px"> </el-table-column>
        <el-table-column
          label="角色名称"
          prop="roleName"
          width="300px"
        ></el-table-column>
        <el-table-column
          label="角色描述"
          prop="roleDesc"
          width="300px"
        ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small">编辑</el-button>
            <el-button type="danger" size="small" @click="del(scope.row.id)"
              >删除</el-button
            >
            <el-button type="warning" size="small" @click="setRights(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加弹框 -->
    <el-dialog title="添加角色" :visible.sync="addDia">
      <el-form :model="addRolesForm" ref="addRolesRef" :rules="addRolesRules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addRolesForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addRolesForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDia = false">取 消</el-button>
        <el-button type="primary" @click="addRolesOk">确 定</el-button>
      </div>
    </el-dialog>

    <!--编辑弹框 -->
    <el-dialog title="编辑角色" :visible.sync="editDia">
      <el-form :model="addRolesForm" ref="addRolesRef" :rules="addRolesRules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addRolesForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addRolesForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDia = false">取 消</el-button>
        <el-button type="primary" @click="editDiaOk">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配权限弹框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="isSetRightDia"
      width="50%"
      :before-close="handleClose"
    >
      <el-tree
        ref="RightTreeRef"
        :data="treeData"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="checkedAll"
        :props="defaultProps"
      >
      </el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isSetRightDia = false">取 消</el-button>
        <el-button type="primary" @click="UpdateCheckedRightOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [], //角色列表的数据
      addRolesForm: {
        //表单数据
        roleName: "",
        roleDesc: "",
      }, //添加角色的表单数据
      addDia: false, //添加角色的弹框
      editDia: false, //编辑角色的弹框
      isSetRightDia: false, //分配权限弹框
      checkedAll: [], //默认选中

      defaultProps: {
        label: "authName",
        children: "children",
      },

      treeData: [], //树的数据
      rids: "",

      addRolesRules: {
        //表单验证
        roleName: [
          { required: true, message: "请输入名字", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15个字符",
            trigger: "change",
          },
        ],
        roleDesc: [
          { required: true, message: "请输入描述", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getRolesList();
  },
  methods: {
    // 渲染列表
    async getRolesList() {
      const res = await this.$API.getUserRolesListApi();
      console.log("roles", res);
      this.rolesList = res;
    },
    // 添加角色
    addRoles() {
      this.addDia = true;
      this.getRolesList();
    },
    // 添加角色的 确定按钮
    addRolesOk() {
      this.$refs.addRolesRef.validate(async (valid) => {
        if (!valid) return false;
        console.log(this.addRolesForm);
        await this.$API.addRolesApi(this.addRolesForm);
        this.getRolesList();
        this.addDia = false;
      });
    },
    // 删除
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await this.$API.delRolesApi(id);
          this.getRolesList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    }, //编辑  确定按钮
    editDiaOk() {},
    // 分配权限
    async setRights(row) {
      // 调用接口
      this.rids = row.id;
      const res = await this.$API.getRightsListApi("tree");
      console.log("分配权限:", res);
      this.treeData = res;

      // 递归获取
      this.getCheckedIdAll(row, this.checkedAll);
      console.log("当前角色所有权限", this.checkedAll);
      this.isSetRightDia = true;
    },
    getCheckedIdAll(row, checkedAll) {
      if (!row.children) {
        return checkedAll.push(row.id);
      }
      row.children.forEach((item) => {
        this.getCheckedIdAll(item, checkedAll);
      });
    },
    // 点击分配权限确定按钮
    async UpdateCheckedRightOk() {
      //  全选id
      const allchecked = this.$refs.RightTreeRef.getCheckedKeys();

      // 半选id
      const halfchecked = this.$refs.RightTreeRef.getHalfCheckedKeys();

      console.log("全选：", allchecked);
      console.log("半选：", halfchecked);

      // 将上面的全选和半选合并
      const mergeCheckedKeysArr = [...allchecked, ...halfchecked];

      // 合并转换成字符串 json
      const checedArrStr = mergeCheckedKeysArr.join(",");

      // 掉角色授权的
      const res = await this.$API.setRightsToRoleApi(this.rids, checedArrStr);
      console.log("更新", res);
      this.getRolesList();
      this.isSetRightDia = false;
    },

    handleClose() {
      this.checkedAll = [];
      this.isSetRightDia = false;
    },
    //删除标签
    delTag() {},
  },
  components: {},
};
</script>

<style lang='scss'>
.el-card {
  margin-top: 20px;
}

.el-tag {
  margin: 10px;
}
.el-row{
  display: flex;
  border: 1px solid #f4f4f5;
}
.col1 {
  width: 20%;
  margin-top: 20px;
  vertical-align: middle;
  line-height: 20px;
}
.col2{
  width: 175.66667%;
  margin: 30px;
}
</style>
